package handler

import (
	"context"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"google.golang.org/protobuf/types/known/emptypb"
	"mxshop_srvs/goods_srv/global"
	"mxshop_srvs/goods_srv/model"
	"mxshop_srvs/goods_srv/proto"
)

// 品牌分类接口
// CategoryBrandList 获取品牌分类列表
func (g *GoodsServer) CategoryBrandList(ctx context.Context, in *proto.CategoryBrandFilterRequest) (*proto.CategoryBrandListResponse, error) {
	// 实现获取品牌分类列表的逻辑
	// 查询品牌分类总数
	categoryBrandListResponse := proto.CategoryBrandListResponse{}
	var total int64
	global.DB.Model(&model.GoodsCategoryBrand{}).Count(&total)
	categoryBrandListResponse.Total = int32(total)
	// 查询品牌分类列表
	var categoryBrandList []model.GoodsCategoryBrand
	global.DB.Preload("Category").Preload("Brands").Scopes(Paginate(int(in.Page), int(in.PagePerNums))).Find(&categoryBrandList)

	var CategoryBrandResponses []*proto.CategoryBrandResponse
	for _, categoryBrand := range categoryBrandList {
		CategoryBrandResponses = append(CategoryBrandResponses, &proto.CategoryBrandResponse{
			Id: int32(categoryBrand.ID),
			Brand: &proto.BrandInfoResponse{
				Id:   int32(categoryBrand.BrandsID),
				Name: categoryBrand.Brands.Name,
				Logo: categoryBrand.Brands.Logo,
			},
			Category: &proto.CategoryInfoResponse{
				Id:             int32(categoryBrand.CategoryID),
				Name:           categoryBrand.Category.Name,
				Level:          categoryBrand.Category.Level,
				IsTab:          categoryBrand.Category.IsTab,
				ParentCategory: int32(categoryBrand.Category.ParentCategoryID),
			},
		})
	}
	categoryBrandListResponse.Data = CategoryBrandResponses
	return &categoryBrandListResponse, nil
}

// GetCategoryBrand 获取分类下的品牌
func (g *GoodsServer) GetCategoryBrand(ctx context.Context, in *proto.CategoryInfoRequest) (*proto.BrandListResponse, error) {
	var brandListResponse proto.BrandListResponse
	// 实现获取分类下的品牌的逻辑
	// 1、根据分类id查询分类是否存在
	category := model.Category{}
	if result := global.DB.Find(&category, in.Id).First(&category); result.RowsAffected == 0 {
		return nil, status.Error(codes.NotFound, "商品分类不存在")
	}
	var categoryBrands []model.GoodsCategoryBrand
	if result := global.DB.Preload("Brands").Where(&model.GoodsCategoryBrand{CategoryID: category.ID}).Find(&categoryBrands); result.RowsAffected > 0 {
		brandListResponse.Total = int32(len(categoryBrands))
	}
	for _, categoryBrand := range categoryBrands {
		brandListResponse.Data = append(brandListResponse.Data, &proto.BrandInfoResponse{
			Id:   categoryBrand.BrandsID,
			Name: categoryBrand.Brands.Name,
			Logo: categoryBrand.Brands.Logo,
		})
	}
	return &brandListResponse, nil
}

// CreateCategoryBrand 创建品牌分类
func (g *GoodsServer) CreateCategoryBrand(ctx context.Context, in *proto.CategoryBrandRequest) (*proto.CategoryBrandResponse, error) {
	// 实现创建品牌分类的逻辑
	var category model.Category
	if result := global.DB.Find(&category, in.CategoryId).First(&category); result.RowsAffected == 0 {
		return nil, status.Error(codes.InvalidArgument, "商品分类不存在")
	}
	var brand model.Brands
	if result := global.DB.Find(&brand, in.BrandId).First(&brand); result.RowsAffected == 0 {
		return nil, status.Error(codes.InvalidArgument, "品牌不存在")
	}
	var categoryBrand model.GoodsCategoryBrand
	categoryBrand.CategoryID = category.ID
	categoryBrand.BrandsID = brand.ID
	global.DB.Create(&categoryBrand)
	return &proto.CategoryBrandResponse{Id: categoryBrand.ID}, nil
}

// DeleteCategoryBrand 删除品牌分类
func (g *GoodsServer) DeleteCategoryBrand(ctx context.Context, in *proto.CategoryBrandRequest) (*emptypb.Empty, error) {
	// 实现删除品牌分类的逻辑
	if result := global.DB.Delete(&model.GoodsCategoryBrand{}, in.Id); result.RowsAffected == 0 {
		return nil, status.Error(codes.NotFound, "品牌分类不存在")
	}
	return &emptypb.Empty{}, nil
}

// UpdateCategoryBrand 更新品牌分类
func (g *GoodsServer) UpdateCategoryBrand(ctx context.Context, in *proto.CategoryBrandRequest) (*emptypb.Empty, error) {
	// 实现更新品牌分类的逻辑
	var categoryBrand model.GoodsCategoryBrand
	if result := global.DB.Find(&categoryBrand, in.Id); result.RowsAffected == 0 {
		return nil, status.Error(codes.NotFound, "品牌分类不存在")
	}
	var category model.Category
	if result := global.DB.Find(&category, in.CategoryId).First(&category); result.RowsAffected == 0 {
		return nil, status.Error(codes.InvalidArgument, "商品分类不存在")
	}
	var brand model.Brands
	if result := global.DB.Find(&brand, in.BrandId).First(&brand); result.RowsAffected == 0 {
		return nil, status.Error(codes.InvalidArgument, "品牌不存在")
	}
	categoryBrand.CategoryID = category.ID
	categoryBrand.BrandsID = brand.ID
	return &emptypb.Empty{}, nil
}
